//
//  XQOrderDetailCell.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/5.
//

import UIKit

class XQOrderDetailCell: UITableViewCell {

    private lazy var logoImageView : UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        lb.adjustsFontSizeToFitWidth = true
        return lb
    }()
    
    private lazy var arrowImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.sizeToFit()
        imageView.image = UIImage(named: "order_arrow_unselect")
        return imageView;
    }()
    
    private lazy var arrowLb : UILabel = {
        let lb = UILabel()
        lb.text = "Authentication"
        lb.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        lb.textAlignment = .center
        lb.textColor = .white
        return lb
    }()
    
    private lazy var lineView : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#EFEFEF")
        return view
    }()
    
    var model : XQOrderDetailPropublicaModel? {
        didSet {
            guard let m = model else {return}
            
            if let url = m.refer {
                self.logoImageView.kf.setImage(with: URL(string: url))
            }
            
            if let title = m.accessing {
                self.titleLb.text = title
            }
            
            if let select = m.orders {
                self.arrowImageView.image = UIImage(named: select == 1 ? "order_arrow_select" : "order_arrow_unselect")
                self.arrowLb.isHidden = select == 1
                
                self.arrowImageView.snp.updateConstraints { make in
                    make.width.equalTo(select == 1 ? 62 : 109)
                }
            }
                       
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        makeUI()
        makeConstraints()
    }

    required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        self.contentView.addSubview(self.logoImageView)
        self.contentView.addSubview(self.titleLb)
        self.contentView.addSubview(self.lineView)
        self.contentView.addSubview(self.arrowImageView)
        self.arrowImageView.addSubview(self.arrowLb)
    }
    
    private func makeConstraints() {
        self.logoImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
            make.height.width.equalTo(48)
        }
        self.arrowImageView.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoImageView)
            make.right.equalTo(-15)
            make.width.equalTo(109)
        }
        self.titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoImageView)
            make.left.equalTo(self.logoImageView.snp.right).offset(10)
            make.right.lessThanOrEqualTo(self.arrowImageView.snp.left).offset(-12)
        }
        self.lineView.snp.makeConstraints { make in
            make.bottom.equalTo(0)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(1)
        }
        self.arrowLb.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

}
